﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
/// <summary>
/// Summary description for ThanhVienDataAccess
/// </summary>
public class ThanhVienDataAccess
{
    private Helper _helper = new Helper();

    public ThanhVien Login(string Username, string Password)
    {
        ThanhVien thanhVien = null;
        string query = string.Format("SELECT * FROM stt_thanhvien WHERE username = '{0}' AND password = '{1}';", Username, Password);
        SqlDataReader reader = _helper.ExecuteReader(query);
        while (reader.Read())
        {
            thanhVien = new ThanhVien()
            {
                Username = (string)reader["username"],
                HinhAnh = (string)reader["hinh"],
                Email = (string)reader["email"],
                HoTen = (string)reader["hoten"],
                IsAdmin = (bool)reader["isadmin"],
                IsActive = (bool)reader["isActive"]
            };
        }                                                                                                     
        return thanhVien;
    }

    public int Register(ThanhVien thanhVienMoi)
    {
        if (isExist(thanhVienMoi.Username, thanhVienMoi.Email))
            return -2;
        string query = string.Format("INSERT INTO stt_thanhvien VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}', '{9}', '{10}')",
           thanhVienMoi.Username, thanhVienMoi.Password, thanhVienMoi.HoTen, thanhVienMoi.DiaChi, thanhVienMoi.SoDienThoai,
           thanhVienMoi.NoiLamViec, thanhVienMoi.NgaySinh, thanhVienMoi.GioiThieu, thanhVienMoi.HinhAnh, 0, thanhVienMoi.Email);
        return _helper.ExecutNonQuery(query);
    }

    public bool isExist(string Username, string Email) 
    {
       string query = string.Format("SELECT * FROM stt_thanhvien WHERE username = '{0}' OR email = '{1}'", Username, Email);
        DataTable dataTable = _helper.GetTable(query);
        if (dataTable.Rows.Count== 0)
           return false;
       return true;
    }

    public int updateAvatar(string url, string Username) 
    {
        string query = string.Format("UPDATE stt_thanhvien SET hinh = '{0}' WHERE username = '{1}'", url, Username);
        return _helper.ExecutNonQuery(query);
    }

}